package com.xxh.securitydemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.securitydemo.dao.UmsMenuDao;
import com.xxh.securitydemo.dao.UmsRoleDao;
import com.xxh.securitydemo.dto.MenuAlloc;
import com.xxh.securitydemo.model.UmsMenu;
import com.xxh.securitydemo.model.UmsRole;
import com.xxh.securitydemo.service.UmsRoleMenuRelationService;
import com.xxh.securitydemo.service.UmsRoleService;
import com.xxh.securitydemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.util.annotation.Nullable;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Resource
    UmsRoleDao umsRoleDao;

    @Autowired
    UmsMenuDao umsMenuDao;

    @Resource
    UmsRoleService umsRoleService;

    @GetMapping("/list")
    public Result listRole(@RequestParam(value = "page" ,defaultValue = "1") @Nullable String page,
                           @RequestParam(value = "size",defaultValue = "5") @Nullable String size){
        IPage<UmsRole> iPage = new Page(Long.valueOf(page), Long.valueOf(size));
        IPage<UmsRole> selectPage = umsRoleDao.selectPage(iPage, null);
        return Result.ok().put("roleData",selectPage);
    }

    @GetMapping("/listMenu")
    public Result listMenu(int roleID){
        List<UmsMenu> list = umsMenuDao.selectMenuByRoleID(roleID);
        return  Result.ok().put("selectedMenu",list);
    }

    @PostMapping("/allocMenu")
    public Result allocMenu(@RequestParam("roleId")int roleId,@RequestParam("menuIds") List<Long> menuIds){
        umsRoleService.allocRoleMenus((long) roleId,menuIds);
        return Result.ok().put("msg","分配完成");
    }

}
